<?php
require_once('config.php');
require_once('functions/general.php');
require_once('functions/sql.php');
require_once('alien/Smarty/Smarty.class.php');
require_once('pages/session.php');

define('ACTION_LOGIN', 'login');
define('ACTION_REGISTER', 'register');
define('ACTION_LOGOUT', 'logout');

define('VIEW_REGISTER', 'register');
define('VIEW_LOGIN', 'login');

session_start();

$smarty = new Smarty(); 
$smarty->template_dir
    = array('templates', 'templates/common', 'templates/account');

$smarty->assign('CONFIG', $CONFIG);
$logged_in = logged_in_status($smarty);
$mysql_link = get_mysql_link();
$tpl = null;

$sess = new Session($smarty, $mysql_link);

if (isset($_POST['action']) or isset($_GET['action'])) {
    $action = isset($_POST['action']) 
        ? $_POST['action'] : $_GET['action'];

    switch ($action) {
        case ACTION_LOGIN:
            list($user, $pass) 
                = array_extract_vars($_POST, array('user', 'password'));
            $sess->login($user, $pass);
            break;

        case ACTION_REGISTER:
            list($user, $pass, $pass_again) 
                = array_extract_vars($_POST, 
                    array('user', 'password', 'password_again'));
            $tpl = $sess->register($user, $pass, $pass_again);
            break;

        case ACTION_LOGOUT:
            $tpl = $sess->logout();
            break;
        default:
            print "Unknown action " . htmlentities($action);
            assert(0);
    }
}

if (isset($_GET['view'])) {
    $view = $_GET['view'];
    switch ($view) {
        case VIEW_REGISTER:
            $tpl = $sess->show_registration();
            break;
        case VIEW_LOGIN:
            $tpl = $sess->show_login_form();
            break;
        default:
            print "Unknown view " . htmlentities($action);
            assert(0);
    }
}

if (is_null($tpl)) {
    $tpl = $sess->show_login_form();
}

$smarty->display($tpl);
mysql_close($mysql_link);	
?>
